<template>
  <div>
    <el-container style="height: 100vh">
      <el-aside width="200px" style="background: rgba(0, 0, 0, 0.8)">
        <!-- 侧边栏 
        background-color="#303C5E" 背景色
        text-color="#fff" 文本色
        active-text-color="#ffd04b" 激活的文本色
        unique-opened	是否只保持一个子菜单的展开
        router	是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
        -->
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="rgba(0,0,0,0.8)"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
        >
          <!-- 动态侧边栏 -->
          <div v-for="item in user.menus" :key="item.id">
            <!-- 目录 -->
            <el-submenu :index="item.id + ''" v-if="item.children">
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  v-for="i in item.children"
                  :key="i.id"
                  :index="i.url"
                  >{{ i.title }}</el-menu-item
                >
              </el-menu-item-group>
            </el-submenu>

            <!-- 菜单 -->
            <el-menu-item :index="item.url" v-else>
              <i class="el-icon-s-data"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>
          </div>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header class="head">
          <div class="header-con">
            <div>{{ user.username }}</div>
            <el-button @click="logout">退出登录</el-button>
          </div>
        </el-header>
        <!-- 面包屑导航 -->
        <el-main>
          <el-breadcrumb
            separator-class="el-icon-arrow-right"
            v-if="$route.meta.title"
          >
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.meta.title }}</el-breadcrumb-item>
          </el-breadcrumb>

          <!-- 二级路由出口 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import {mapActions,mapGetters} from 'vuex';

export default {
  data() {
    return {
      username: "",
    };
  },
  computed: {
    ...mapGetters({
      user:'user'
    })
  },
  methods: {
    ...mapActions({
      changeUser: "changeUser",
    }),
    // 退出登录
    logout() {
      //清除vuex和本地存储的用户信息
      this.changeUser({});
      //跳转路由
      this.$router.replace("/login");
    },
  },
  // mounted() {
  //   // 获取账号信息
  //   this.username = JSON.parse(localStorage.getItem("user")).username;
  // },
};
</script>

<style>
.header-con {
  float: right;
}
.head {
  background: #ccc;
}
</style>
